%% Logic with Vectors

%A vector of random numbers:
N=1000;
A=10*rand(N,1)

%Find number of elements in a range
BinStart=1.5;
BinEnd=2.5;

%These are all the elements greater than BinStart
%Note the result of the logical operations is 1 or 0
M1=A>BinStart

%These are all the elements less than or equal to BinEnd
M2=A<=BinEnd

%These are elements that meet both condions
W=M1&M2

%Now count them up:
NBin=sum(W)

%% Finding Pi with Random numbers

%Make a vector of X and Y values made from random numbers between -L, L
N=10000;
L=1;

X=2*L*rand(N,1)-L;
Y=2*L*rand(N,1)-L;

%Take a look...
figure
plot(X,Y,'.')
xlabel('X')
ylabel('Y')
axis equal
%Points are uniformly distributed throughout the Square

%The distance from origin is:
R=sqrt(X.^2+Y.^2)

%The number of points within a circle of radius r=L centered at origin is
Ncircle=sum(R<L)

%Area of square:
Area_Sq=(2*L).^2;

%An estimate of the area of the circle is:
Area_C=Ncircle/N*Area_Sq

%Since we know A=pi*r^2 for circle:
OurPi=Area_C/L.^2

%% Let's check how well this works

L=1;

Nvec=logspace(2,8,100);
OurPi=zeros(size(Nvec));

%Make estimate of pi with increasing N
for nn=1:length(Nvec)
    N=round(Nvec(nn));
    X=2*L*rand(N,1)-L;
    Y=2*L*rand(N,1)-L;
    R=sqrt(X.^2+Y.^2);
    Ncircle=sum(R<L);
    Area_Sq=(2*L).^2;
    Area_C=Ncircle/N*Area_Sq;
    
    OurPi(nn)=Area_C/L.^2;
    
end

%Plot
figure
loglog(Nvec,OurPi-pi,'.')
xlabel('Number of Points')
ylabel('Deviation from \pi')

%Does not seem like the best way to calculate pi






